package com.jt.common.exception;

import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;

import com.jt.common.response.Response;
import com.jt.common.response.ResponseCode;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;

/**
 * 总体的异常
 */

@RestControllerAdvice
public class GlobalExceptionHandler  {
	public Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
	

	@ExceptionHandler({ RestException.class })
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Response RestExceptionHandler(HttpServletResponse response, RestException e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		return new Response(e.getCode(),e.getMessage());
	}
	

	
	@ExceptionHandler({ IllegalArgumentException.class })
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Response IllegalArgumentExceptionHandler(HttpServletResponse response, IllegalArgumentException e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		return new Response(ResponseCode.ILLEGAL_PARAM,e.getMessage());
	}
	
	@ExceptionHandler({ MissingServletRequestParameterException.class })
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Response MissingServletRequestParameterException(HttpServletResponse response, MissingServletRequestParameterException e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		return new Response(ResponseCode.ILLEGAL_PARAM,e.getMessage());
	}
	
	@ExceptionHandler({ MethodArgumentTypeMismatchException.class })
    @ResponseBody
    @ResponseStatus(value = HttpStatus.OK)
    public Response MethodArgumentTypeMismatchExceptionHandler(HttpServletResponse response, MethodArgumentTypeMismatchException e) {
        response.setCharacterEncoding("utf-8");
        logger.error(e.getMessage(), e);
        return new Response(ResponseCode.ILLEGAL_PARAM,e.getMessage());
    }
	
	@ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    @ResponseBody
    @ResponseStatus(value = HttpStatus.OK)
    public Response<?> HttpRequestMethodNotSupportedExceptionHandler(HttpServletResponse response, HttpRequestMethodNotSupportedException e) {
        response.setCharacterEncoding("utf-8");
        logger.error(e.getMessage(), e);
        return new Response<>(ResponseCode.ILLEGAL_METHOD,e.getMessage());
    }
	
	@ExceptionHandler({ SQLException.class })
	@ResponseStatus(value = HttpStatus.OK)
	public Response SQLExceptionHandler(HttpServletResponse response, SQLException e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		return new Response(ResponseCode.SQL_EXCEPTION,e.getMessage());
	}
	
	@ExceptionHandler({ Exception.class })
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Response ExceptionHandler(HttpServletResponse response, Exception e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		return new Response(ResponseCode.UNKOWN_EXCEPTION,e.getMessage());
	}
	
	@ExceptionHandler({ InvalidFormatException.class })
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Response InvalidFormatExceptionHandler(HttpServletResponse response, InvalidFormatException e) {
		response.setCharacterEncoding("utf-8");
		logger.error(e.getMessage(), e);
		 return new Response<>(ResponseCode.ILLEGAL_METHOD,e.getMessage());
	}
	
	
//	@InitBinder
//    public void initBinder(WebDataBinder binder) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//    }

}
